package com.gnerv.sylvanas.business.manage.operation;

import com.gnerv.sylvanas.framework.core.BaseAccountPassword;
import com.gnerv.sylvanas.framework.core.BaseCurrentAccount;
import com.gnerv.sylvanas.business.manage.model.bo.AccountBO;
import com.gnerv.sylvanas.business.manage.model.dto.AccountDTO;
import com.gnerv.sylvanas.business.manage.model.dto.AccountDetailsDTO;
import com.gnerv.sylvanas.business.manage.model.entity.Account;
import com.gnerv.sylvanas.business.manage.storage.AccountStorageService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Set;

import static com.gnerv.sylvanas.framework.common.tools.PageTools.limitSize;
import static com.gnerv.sylvanas.framework.common.tools.PageTools.limitStart;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class AccountOperationService {

    private final AccountStorageService accountStorageService;
    private final BaseAccountPassword baseAccountPassword;
    private final BaseCurrentAccount baseCurrentAccount;

    
    public boolean create(AccountBO accountBO) {
        Account account = accountBO.toAccount();
        account.setPassword(baseAccountPassword.encrypt(account.getPassword()));
        
        accountStorageService.insert(account);
        return true;
    }

    
    public boolean deleteByUkId(String ukId) {
        accountStorageService.deleteByUkId(ukId);
        accountStorageService.unbindRole(ukId);
        return true;
    }

    
    public boolean updateByUkId(AccountBO accountBO) {
        Account account = accountBO.toAccount();
        accountStorageService.updateByUkId(account);
        return true;
    }

    
    public AccountDTO selectByUkId(String ukId) {
        return accountStorageService.selectByUkId(ukId);
    }

    
    public AccountDetailsDTO selectDetailsByUkId(String ukId) {
        return accountStorageService.selectDetailsByUkId(ukId);
    }

    
    public AccountDTO selectByUsername(String username) {
        return accountStorageService.selectByUsername(username);
    }

    
    public AccountDetailsDTO selectDetailsByUsername(String username) {
        return accountStorageService.selectDetailsByUsername(username);
    }

    
    public List<AccountDTO> selectListByUsername(String username, int page, int size) {
        return accountStorageService.selectListByUsername(username, limitStart(page, size), limitSize(size));
    }

    
    public List<AccountDTO> selectListByRoleUkId(String roleUkId) {
        return accountStorageService.selectListByRoleUkId(roleUkId);
    }

    
    public List<AccountDTO> selectListByOrgUkId(String orgUkId) {
        return accountStorageService.selectListByOrgUkId(orgUkId);
    }

    
    public boolean bindRole(String accountUkId, Set<String> roleUkIdList) {
        if (ObjectUtils.isEmpty(accountUkId) || CollectionUtils.isEmpty(roleUkIdList)) {
            return true;
        }
        accountStorageService.bindRole(accountUkId, roleUkIdList, baseCurrentAccount.ukId());
        return true;
    }

    
    public boolean unbindRole(@NonNull String accountUkId) {
        accountStorageService.unbindRole(accountUkId);
        return true;
    }

    
    public boolean bindOrg(String accountUkId, String orgUkId) {
        accountStorageService.bindOrg(accountUkId, orgUkId, baseCurrentAccount.ukId());
        return true;
    }

    
    public boolean unbindOrg(@NonNull String accountUkId) {
        accountStorageService.unbindOrg(accountUkId);
        return true;
    }

    
    public int selectCountByUsername(String username) {
        return accountStorageService.selectCountByUsername(username);
    }

    
    public Set<String> selectListUkIdByRoleUkIdAndOrgUkId(Set<String> roleUkIdList, Set<String> orgUkIdList) {
        return accountStorageService.selectListUkIdByRoleUkIdAndOrgUkId(roleUkIdList, orgUkIdList);
    }

    
    public List<AccountDetailsDTO> selectListDetailsByOrgUkId(String orgUkId) {
        return accountStorageService.selectListDetailsByOrgUkId(orgUkId);
    }

    
    public List<AccountDetailsDTO> selectListDetailsByUsername(String username, int page, int size) {
        return accountStorageService.selectListDetailsByUsername(username, limitStart(page, size), limitSize(size));
    }

}




